package com.feicheng.lanniao.service.impl;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feicheng.lanniao.entity.QuotaRecord;
import com.feicheng.lanniao.mapper.QuotaRecordMapper;
import com.feicheng.lanniao.result.ResultInfo;
import com.feicheng.lanniao.service.QuotaRecordService;

@Service
public class QuotaRecordServiceImpl extends ServiceImpl<QuotaRecordMapper, QuotaRecord> implements QuotaRecordService {
	@Override
	public ResultInfo updateQuota(Integer clientId, Integer orderTypeId) {
		// TODO Auto-generated method stub
		QueryWrapper<QuotaRecord> wrapper = new QueryWrapper<QuotaRecord>();
		wrapper.eq("client_id", clientId).eq("order_type_id", orderTypeId);
		QuotaRecord qRecord = this.getOne(wrapper);
		qRecord.setQuota(10);
		return ResultInfo.success(this.updateById(qRecord));
	}

	@Override
	public ResultInfo getQuota(Integer clientId, Integer orderTypeId) {
		// TODO Auto-generated method stub
		QueryWrapper<QuotaRecord> wrapper = new QueryWrapper<QuotaRecord>();
		wrapper.eq("client_id", clientId).eq("order_type_id", orderTypeId);
		QuotaRecord qRecord = this.getOne(wrapper);
		if(null == qRecord) {
			QuotaRecord quoRecord = new QuotaRecord();
			quoRecord.setClientId(clientId);
			quoRecord.setOrderTypeId(orderTypeId);
			this.save(quoRecord);
			quoRecord.setQuota(-1);
			return ResultInfo.success(quoRecord);
		} else {
			if(qRecord.getQuota()>0) {
				qRecord.setQuota(qRecord.getQuota()-1);
				this.updateById(qRecord);
				return ResultInfo.success(this.getById(qRecord.getId()));
			} else {
				qRecord.setQuota(-1);
				return ResultInfo.success(qRecord);
			}
		}
		
	}

	@Override
	public ResultInfo initQuota(Integer clientId) {

		for (int i = 9; i < 17; i++) {
			QuotaRecord quotaRecord = new QuotaRecord();
			quotaRecord.setClientId(clientId);
			quotaRecord.setOrderTypeId(i);
			quotaRecord.setQuota(0);
			this.save(quotaRecord);
		}
		return ResultInfo.success();
	}

}
